{extend name="layout/tpl" /}

{block name="title"}新闻添加{/block}

{block name="content"}
<form class="layui-form" action="" style="margin-top: 15px;">
    <div class="layui-form-item">
        <label class="layui-form-label">标题:</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">分类:</label>
        <div class="layui-input-block">
            <select name="category">
                {foreach name="tree" item="tr"}
                <option value="{$tr.id}">{$tr.name}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">关键字:</label>
        <div class="layui-input-block">
            <input type="text" name="keywords" autocomplete="off" placeholder="请输入关键字,多个关键字用逗号分割" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">描述:</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入描述" name="description" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">内容:</label>
        <div class="layui-input-block">
            <textarea name="content" id="content"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="submit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

{/block}

{block name="footerjs"}
<?php
$url = url('admin.news.add');
$formsubmit = <<<HEAR
var index;
console.log(data.field.content);
$.ajax({
    type:       'post',
    url:        "$url",
    data:       data.field,
    dataType:   'json',
    beforeSend: function () {
        index = layer.load(0, {shade: false});
    },
    success:    function (msg) {
        layer.close(index);
        if(msg.errCode === "0") {
            layer.msg('成功');
            setTimeout(function () {
                parent.location.reload();
            }, 500)
        } else {
            layer.msg(msg.msg);
        }
    }
});
return false;
HEAR;
?>
<?php $editor = app('config')->get('editor');if($editor['name'] == 'ueditor'): ?>
<script src="{:url('/')}ueditor/ueditor.config.js"></script>
<script src="{:url('/')}ueditor/ueditor.all.min.js"></script>
<script src="{:url('/')}ueditor/lang/zh-cn/zh-cn.js"></script>
<script>
    var ue = UE.getEditor('content',{
        serverUrl :"{:url('admin.news.ueditor')}",
        initialFrameHeight:450
    });
    layui.use(['form'], function () {
        var form = layui.form
            ,layer = layui.layer
            ,$ = layui.jquery
        ;
        form.on('submit(submit)', function (data) {
            {php} echo $formsubmit;{/php}
        });
    });
</script>
<?php else: ?>
<script>
    layui.use(['form', 'layedit'], function () {
        var form = layui.form
            ,layer = layui.layer
            ,$ = layui.jquery
            ,layedit = layui.layedit;
        ;
        layedit.set({
            uploadImage: {
                url: "{:url('admin.news.upload')}"
            }
        });
        var edit = layedit.build('content'); //建立编辑器
        form.on('submit(submit)', function (data) {
            data.field.content = layedit.getContent(edit);
            {php} echo $formsubmit;{/php}
        });
    });
</script>
<?php endif; ?>
{/block}